<?php
/**
 * 广告类
 */

class Advert extends MyBase{
	
	function __construct() {
		parent::__construct('advert.id', array('keyword' => 'title'));
	}
	
	/**
	 * 修改广告
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('posid' => $post['posid'], 'title' => $post['title']);
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){return lang('already').lang('exist');}
		if($id > 0){
			$rs = $this->get_row($id, '`path`');
		}
		$data = array(
			'posid'				=> $post['posid'],
			'title'				=> $post['title'],
			'url'				=> $post['url'],
			'related_news'		=> $post['related_news'],
			'show'				=> empty($post['show']) ? 0 : 1,
			'rank'				=> $post['rank'],
			'brief'				=> $post['brief'],
			'related_goods'		=> $post['related_goods'],
			'begintime'			=> strtotime($post['begintime']),
			'endtime'			=> strtotime($post['endtime']),
		);
		
		$oUpload = new Upload(array(
			'upload_form_field'	=> 'advert_img',
			'auto_date_dir'		=> true,
			'out_file_dir'		=> UP_PTH.'advert/',
			'allowed_file_ext'	=> array('gif', 'jpg', 'jpeg', 'png'),
		));
		$img_arr = $oUpload->upload_process();
		if(!empty($img_arr)){
			$_f_adpos = cache_read('adposs.php');
			$pos_info = $_f_adpos['list_id'][$post['posid']];

			if(isset($rs) && !empty($rs)){	// 删除旧图
				@unlink(UP_PTH.$rs['path']);
				@unlink(UP_PTH.'thumb/'.$rs['path']);
				/*foreach ($_f_adpos['list_id'] as $key => $posinfo) {
					@unlink(UP_PTH."thumb/{$pos_info['width']}_{$pos_info['height']}/".$rs['path']);
				}*/
			}
			$img_path = array_pop($img_arr);			
			// File::resize($img_path, 200, 150, 'adaptive');	// 缩小至合适大小、截取
			// if($pos_info['width'] != 200 && $pos_info['height'] != 150){
				File::resize($img_path, $pos_info['width'], $pos_info['height'], 'adaptive');	// 缩小至合适大小、截取
			// }
			$data['path'] = str_replace(UP_PTH, '', $img_path);
		}
		$data['addtime'] = empty($post['addtime']) ? TIME : strtotime($post['addtime']);
		if($id > 0){
			$data['updatetime'] = empty($post['updatetime']) ? TIME : strtotime($post['updatetime']);
			$id = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$id = $this->db->insert($this->tbname, $data);
		}
		if($id > 0){
			return $this->cache_advert();
		}
	}
	
	/**
	 * 更新排序
	 */
	public function rank($post){
		if(!is_array($post['rank']))return false;
		$iRtn = 0;
		foreach($post['rank'] as $id => $rank){
			if($this->db->update($this->tbname, array('id' => $id), array(
				'rank'		 => $rank,
				'updatetime' => TIME,
			))){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_advert();
		}
		return $iRtn;
	}

	/**
	 * 删除广告
	 */
	public function del($post){
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		$iRtn = 0;
		foreach($post['list'] as $id){
			$rs = $this->get_row($id, '`path`,`base`,`ext`');
			if($this->db->delete($this->tbname, array('id' => $id))){
				$iRtn ++;
			}
			// 删除图片
			@unlink(UP_PTH.$rs['path'].'/'.$rs['base'].'.'.$rs['ext']);
			@unlink(UP_PTH.$rs['path'].'/'.$rs['base'].'[200-120].'.$rs['ext']);
			@unlink(UP_PTH.$rs['path'].'/'.$rs['base'].'[960-260].'.$rs['ext']);
			@unlink(UP_PTH.$rs['path'].'/'.$rs['base'].'[960-90].'.$rs['ext']);
			@unlink(UP_PTH.$rs['path'].'/'.$rs['base'].'[42-24].'.$rs['ext']);
		}
		return $this->cache_advert();
	}
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND `title` LIKE '%{$select['keyword']}%'";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	/************************************************************************
	 * 取特定列表，前台调用
	 ************************************************************************/
	
	public function get_list_by_pos($posid, $size = 8){
		return $this->get_list('*', array('posid' => $posid, 'show' => true), array('rank' => 'DESC', 'id' => 'DESC'), 1, $size);
	}

	public function get_list_by_mark($mark, $size = 8){
		return $this->get_list('*', array('mark' => $mark, 'show' => true), array('rank' => 'DESC', 'id' => 'DESC'), 1, $size);
	}

	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	public function cache_advert(){
		$all = $this->get_all('*', array('isdel' => 0), array('rank' => 'DESC', 'id' => 'DESC'));
		if(!empty($all['list'])){
			$_f_adpos = cache_read('adposs.php');

			$pos_ads_id = array();
			$pos_ads_mark = array();
			foreach ($all['list'] as $key => $advert_info) {
				$pos_info = $_f_adpos['list_id'][$advert_info['posid']];
				if(empty($pos_ads_id[$pos_info['id']]))$pos_ads_id[$pos_info['id']] = array();
				if(empty($pos_ads_mark[$pos_info['mark']]))$pos_ads_mark[$pos_info['mark']] = array();
				$pos_ads_id[$pos_info['id']][$advert_info['id']] = $advert_info;
				$pos_ads_mark[$pos_info['mark']][$advert_info['id']] = $advert_info;
			}
		}
		return cache_write('adverts.php', array('pos_ads_id' => $pos_ads_id, 'pos_ads_mark' => $pos_ads_mark, 'list' => $all['list']));
	}
}
?>